package com.useeinfo.oa.modules.security.util;

import com.alibaba.fastjson.JSON;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.config.ConstantKeySession;
import com.useeinfo.oa.common.utils.Jedis.JedisTools;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.security.entity.SystemUser;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * 获取session数据
 */
public class SessionKeyUtils {

    public static final String HTTP_HEADER_AUTHORIZATION = "Authorization";

    public static void setSessionUser(HttpServletRequest request, SystemUser systemUser) {
        request.getSession().setAttribute(ConstantKeySession.USER_KEY, systemUser);
        //setRedisUser(systemUser);
    }

    public static SystemUser getSessionUser(HttpServletRequest request) {
        SystemUser systemUser = (SystemUser) request.getSession().getAttribute(ConstantKeySession.USER_KEY);
     /*   if(null == systemUser||null == systemUser.getId()){
            String usetMsg = getRedisUser(request);
            if(StringConverters.null2String(usetMsg).equals("")){
                systemUser = null;
            }else {
               systemUser = JSON.parseObject(usetMsg,SystemUser.class);
            }
        }*/
        return systemUser;
    }

    public static void setSessionEnterprise(HttpServletRequest request, Enterprise enterprise) {
        request.getSession().setAttribute(ConstantKeySession.ENTERPRISE, enterprise);
       /* String authorization = request.getHeader(HTTP_HEADER_AUTHORIZATION);
        if(!StringConverters.null2String(authorization).equals("")){
           setRedisEnterprice(authorization+"enterprice",enterprise);
        }*/

    }

    public static Enterprise getSessionEnterprise(HttpServletRequest request) {
        Enterprise enterprise = (Enterprise) request.getSession().getAttribute(ConstantKeySession.ENTERPRISE);
       /* if(null == enterprise||null == enterprise.getId()){
            String enterpriseMsg = getRedisEnterprice(request);
            if(StringConverters.null2String(enterpriseMsg).equals("")){
                enterprise = null;
            }else {
                enterprise = JSON.parseObject(enterpriseMsg,Enterprise.class);
            }
        }*/
        return enterprise;
    }

    public static Long getSessionUserId(HttpServletRequest request) {
        SystemUser systemUser = (SystemUser) request.getSession().getAttribute(ConstantKeySession.USER_KEY);
        return systemUser == null ? null : systemUser.getId();
    }

    /***用户登录信息存放reids 缓存中****/
    public static void setRedisUser(SystemUser systemUser) {
        JedisTools jedisTools = new JedisTools();
        jedisTools.setKey(StringConverters.null2String(systemUser.getUniqueid()), systemUser.toLoginJedisJSONObject().toJSONString());
    }
    public static String  getRedisUser(HttpServletRequest request) {
        JedisTools jedisTools = new JedisTools();
        return jedisTools.getKey(request.getHeader(HTTP_HEADER_AUTHORIZATION));
    }
    /***用户登录企业信息存放reids 缓存中****/
    public static void setRedisEnterprice(String key,Enterprise enterprice) {
        JedisTools jedisTools = new JedisTools();
        jedisTools.setKey(key, enterprice.toJSONObject().toJSONString());
    }
    public static String  getRedisEnterprice(HttpServletRequest request) {
        JedisTools jedisTools = new JedisTools();
        return jedisTools.getKey(StringConverters.null2String(request.getHeader(HTTP_HEADER_AUTHORIZATION))+"enterprice");
    }
}
